#include "common.hpp"

int main()
{
    // 创建命名管道
    bool ret = createFifo(NAMED_PIPE);
    assert(ret);
    (void)ret;

    // 打开命名管道
    cout << "opening..." << endl;
    int rfd = open(NAMED_PIPE, O_RDONLY);
    cout << "opened" << endl;

    if(rfd < 0) 
    {
        cerr << "errno : " << errno << " strmsg : "<< strerror(errno) << endl;
        exit(1);
    }


    while(true)
    {
        char buffer[1024];
        ssize_t n = read(rfd, buffer, sizeof(buffer));

        if(n > 0)
        {
            buffer[n] = '\0';
            cout << "client -> server# :" << buffer << endl;
        }
        else if(n == 0)
        {
            cout << "client quit! me too!" << endl;
            break;
        }
        else
        {
            cout << "error_no : " << errno << "err_msg : " << strerror(errno) << endl; 
            break;
        }
    }

    close(rfd);

    // 删除命名管道
    removeFifo(NAMED_PIPE);

    return 0;
}